package com.cykj.v_m_sprintboot.controller;

import com.cykj.v_m_sprintboot.bean.TbInput;
import com.cykj.v_m_sprintboot.bean.TbInventory;
import com.cykj.v_m_sprintboot.bean.TbLoss;
import com.cykj.v_m_sprintboot.service.TbInventoryService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TbInventoryController {
    @Autowired
    private TbInventoryService tbInventoryService;
    //获取盘点清单
    @RequestMapping("getInventoryList")
    @ResponseBody
    public Map<String,Object> getInventoryList(@RequestBody TbInventory tbInventory){
        System.out.println("当前页："+tbInventory.getPage()+",几条数据为一页："+tbInventory.getSize());
        RowBounds row =new RowBounds((tbInventory.getPage()-1)*tbInventory.getSize(),tbInventory.getSize());
        Map<String,Object> map=new HashMap<>();
        int count=tbInventoryService.countInventoryInfo(tbInventory);
        if (count<=0){
          tbInventoryService.addInventoryInfo();
        }
        List<TbInventory> getInventoryList=tbInventoryService.getInventoryList(tbInventory,row);
        map.put("count",count);
        map.put("getInventoryList",getInventoryList);
        return map;
    }
    //录入盘点数据
    @RequestMapping("InputInventoryData")
    @ResponseBody
    public String InputInventoryData(@RequestBody TbInventory tbInventory){
       TbInventory tb =tbInventoryService.inventoryInfo(tbInventory.getGoodsName());
       if (tb==null){
           tbInventory.setGoodsUnit("件");
           tbInventory.setInventorySurplus(tbInventory.getInventoryNum()-tbInventory.getGoodsNum());
           tbInventoryService.addLoneData(tbInventory);
           tb =tbInventoryService.inventoryInfo(tbInventory.getGoodsName());
       }
        tbInventory.setInventoryNum(tb.getInventoryNum()+tbInventory.getInventoryNum());
        double num=tbInventory.getInventoryNum()-tb.getGoodsNum();
       if (num<=0){
           tbInventory.setInventorySurplus(num);
           tbInventory.setInventoryLoss(tb.getGoodsNum()-tbInventory.getInventoryNum());
       }else {
           tbInventory.setInventorySurplus(num);
//           tbInventory.setInventoryLoss(num);
       }
        tbInventoryService.updateInventoryInfo(tbInventory);
        return "录入成功";

    }

    //
//





}
